#ifndef STATUSNOTIFIERITEMSOURCE_H
#define STATUSNOTIFIERITEMSOURCE_H

#include <QObject>
#include <QTimer>
#include <QIcon>
#include <QPixmap>
#include <QGSettings>
#include <dbusmenu-qt5/dbusmenuimporter.h>
#include "statusnotifieriteminterface.h"

/*! \brief specialized DBusMenuImporter to correctly create actions' icons based
 * on name
 */
class UKUIMenuImporter : public DBusMenuImporter
{
public:
    using DBusMenuImporter::DBusMenuImporter;

protected:
    virtual QIcon iconForName(const QString & name) override
    {
        return QIcon::fromTheme(name);
    }
};

class StatusNotifierItemSource : public QObject
{
    Q_OBJECT
public:
    explicit StatusNotifierItemSource(QString service, QObject *parent = nullptr);
    ~StatusNotifierItemSource();
    //鼠标动作
    void activate(int x, int y);

    /**
    * These are the standard sizes for icons.
    */
    enum StdSizes {
        SizeSmall = 16,
        SizeSmallMedium = 22,
        SizeMedium = 32,
        SizeLarge = 48,
        SizeHuge = 64,
        SizeEnormous = 128
    };
    Q_ENUM(StdSizes)

signals:
    /**
    * Item属性数据更新时，发送 dataUpdated() 信号
    **/
    void dataUpdated(const QString &serviceName, StatusNotifierItemSource *itemSource, const QMap<uint, QVariant> &data);
//    void contextMenuReady();

private Q_SLOTS:
    void refreshTitle();
    void refreshIcons();
    void refreshToolTip();
    void refresh();
    void performRefresh();
    void syncStatus(QString status);
    void refreshCallback(QDBusPendingCallWatcher *call);
//    void activateCallback(QDBusPendingCallWatcher *);

private:
    void initGsetting();
    QPixmap UKUIDBusImageStructToPixmap(const UKUIDBusImageStruct &image) const;
    QIcon imageVectorToPixmap(const UKUIDBusImageVector &vector) const;
    QIcon iconNameToIcon(QString iconName) const;
    void getBlackThemeIcon(QImage &image) const;
    void overlayIcon(QIcon *icon, QIcon *overlay);

    void setData(uint propertyName, QVariant propertyData);
    QVariant data(uint propertyName) const;



private:
    QGSettings *m_themeSettings;
    QString m_service;
    org::kde::StatusNotifierItem *m_statusNotifierItemInterface;
    QTimer m_refreshTimer;
    bool m_valid = false;
    bool m_isTitleUpdate;
    bool m_isIconUpdate;
    bool m_isTooltipUpdate;
    bool m_isStatusUpdate;
    bool m_refreshing;
    bool m_needsReRefreshing;
    DBusMenuImporter *m_menuImporter;
    QMap<uint, QVariant> m_itemData;

};

#endif // STATUSNOTIFIERITEMSOURCE_H
